Skip to content

Support chained SeekHead elements in MatroskaExtractor - #3597

Open
Tripplesixty wants to merge 1 commit into
androidx:mainfrom
Tripplesixty:main
Open

Tripplesixty wants to merge 1 commit into
androidx:mainfrom
Tripplesixty:main

Conversation

@Tripplesixty

Copy link
Copy Markdown

RFC 9559 Section 6.3 states that a Matroska Segment may contain a primary SeekHead near the beginning of the file that references a single secondary SeekHead (typically written near the end of the file by older versions of mkvmerge) containing the positions of Tracks and/or Cues.

Previously, MatroskaExtractor only recorded Seek entries for ID_TRACKS and ID_CUES, ignoring ID_SEEK_HEAD entries. As a result, files where Cues (or Tracks) are only indexed in the secondary SeekHead emitted an Unseekable SeekMap and could not be sought.

This change:

  • Records the first in-bounds ID_SEEK_HEAD Seek entry position within the Segment (ignoring out-of-range offsets and unknown Segment sizes).
  • On encountering the first Cluster, seeks once to the secondary SeekHead if Tracks or Cues are still needed (respecting FLAG_DISABLE_SEEK_FOR_CUES).
  • After parsing the secondary SeekHead (or recovering if the pointer lands on a non-SeekHead element), proceeds to seek for Tracks/Cues or emits an Unseekable SeekMap, sends track formats once, and returns to the Cluster.
  • Resets pending SeekHead discovery state in seek() when a SeekMap has not yet been sent.

Testing:

  • Added sample_with_chained_seek_head.mkv (derived from sample.mkv by moving SeekHead entries for Info, Tracks, Cues, and Tags into a secondary SeekHead at the end of the Segment referenced by the primary SeekHead) and parameterized ExtractorAsserts coverage in MatroskaExtractorTest reusing extractordumps/mkv/sample.mkv.
  • Added non-parameterized tests in MatroskaExtractorNonParameterizedTest covering FLAG_DISABLE_SEEK_FOR_CUES, a malformed SeekHead pointer targeting a Cluster, a pointer past the Segment end, and a secondary SeekHead without a Cues entry.

// The secondary SeekHead pointer landed on a non-SeekHead element; stop retrying and
// proceed with tracks/seek-map setup so the extractor returns to the Cluster.
finishSecondarySeekHead();
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When id != ID_SEEK_HEAD, control still falls through into switch (id) for the unexpected element, so a bogus pointer landing on ID_SEGMENT, ID_TRACK_ENTRY, or ID_CHAPTER_ATOM can throw or mutate parser state before seeking back. Returning early after finishSecondarySeekHead() avoids running switch (id) on that element, though DefaultEbmlReader will still have pushed that master element onto its stack until its end position is passed.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In startMasterElement / endMasterElement I added an early return after finishSecondarySeekHead() when id != ID_SEEK_HEAD, and tracked ignoredSecondarySeekHeadElementId so endMasterElement skips the matching pop from DefaultEbmlReader's stack (which otherwise caused ID_TRACK_ENTRY to hit checkNotNull(this.currentTrack)).

continueReading = reader.read(input);
if (continueReading
&& (maybeSeekForTracks(seekPosition, input.getPosition())
&& (maybeSeekForSeekHead(seekPosition, input.getPosition())

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If secondarySeekHeadPosition lands on a non-master element near EOF (such as trailing Void padding) or a truncated secondary SeekHead, reader.read(input) returns false before startMasterElement or endMasterElement(ID_SEEK_HEAD) can run. The loop then exits to RESULT_END_OF_INPUT with seekPositionAfterReadingSeekHead still set, never calling finishSecondarySeekHead() or seeking back to the first cluster (and leaving deferred track formats unsent). Recovering in the !continueReading branch when seekPositionAfterReadingSeekHead != C.INDEX_UNSET would let playback still proceed from the cluster.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a recovery in read()'s !continueReading branch when seekPositionAfterReadingSeekHead != C.INDEX_UNSET to call finishSecondarySeekHead() and return Extractor.RESULT_SEEK back to the first Cluster.

RFC 9559 Section 6.3 states that a Matroska Segment may contain a primary
SeekHead near the beginning of the file that references a single
secondary SeekHead (typically written near the end of the file by muxers
such as mkvmerge) containing the positions of Tracks and/or Cues.

Previously, MatroskaExtractor only recorded Seek entries for ID_TRACKS
and ID_CUES, ignoring ID_SEEK_HEAD entries. As a result, files where
Cues (or Tracks) are only indexed in the secondary SeekHead emitted an
Unseekable SeekMap and could not be sought.

This change:
- Records the first in-bounds ID_SEEK_HEAD Seek entry position within
  the Segment (ignoring out-of-range offsets and unknown Segment sizes).
- On encountering the first Cluster, seeks once to the secondary
  SeekHead if Tracks or Cues are still needed (respecting
  FLAG_DISABLE_SEEK_FOR_CUES).
- After parsing the secondary SeekHead (or recovering if the pointer
  lands on a non-SeekHead element), proceeds to seek for Tracks/Cues or
  emits an Unseekable SeekMap, sends track formats once, and returns to
  the Cluster.
- Resets pending SeekHead discovery state in seek() when a SeekMap has
  not yet been sent.

Testing:
- Added sample_with_chained_seek_head.mkv (derived from sample.mkv by
  moving SeekHead entries for Info, Tracks, Cues, and Tags into a
  secondary SeekHead at the end of the Segment referenced by the primary
  SeekHead) and parameterized ExtractorAsserts coverage in
  MatroskaExtractorTest reusing extractordumps/mkv/sample.mkv.
- Added non-parameterized tests in MatroskaExtractorNonParameterizedTest
  covering FLAG_DISABLE_SEEK_FOR_CUES, a malformed SeekHead pointer
  targeting a Cluster, a pointer past the Segment end, and a secondary
  SeekHead without a Cues entry.
@Tripplesixty

Copy link
Copy Markdown
Author

@sahuadarsh0 Thanks for the feedback, both of the edge cases should now be fixed.

@icbaker

icbaker commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

See #3588 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants